package com.vids.service.impl;

import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.ObjectUtil;
import com.vids.mapper.ApeStatusMapper;
import com.vids.mapper.SystemStatusMapper;
import com.vids.mapper.VIIDServerMapper;
import com.vids.model.entity.ApeStatus;
import com.vids.model.entity.SystemStatus;
import com.vids.model.entity.VIIDServer;
import com.vids.runner.VIIDRunner;
import com.vids.service.IBaseService;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;

@RequiredArgsConstructor
@Service
public class BaseServiceImpl implements IBaseService {
    @Value("${update-status.time-long}")
    String timeLong;
   private final VIIDServerMapper viidServerMapper;
   private final SystemStatusMapper systemStatusMapper;
   private final ApeStatusMapper apeStatusMapper;
   private final VIIDRunner viidRunner;
    @Override
    public int updateAndInsertSystemStatus(String inOnline, String deviceId) {
        SystemStatus status = systemStatusMapper.findByVIIDServerID(deviceId);
        VIIDServer viidServer = viidServerMapper.findByVIIDServerID(viidRunner.getSelfViidServerId());
        if (deviceId.equals(viidServer.getUpServerID())) {
            return 1;
        }
        if (ObjectUtil.isNull(status)) {
            SystemStatus newStatus = new SystemStatus();
            newStatus.setId(IdUtil.fastSimpleUUID());
            newStatus.setViidServerId(deviceId);
            newStatus.setIsOnline(inOnline);
            newStatus.setCurrentTime(new Date());
            return systemStatusMapper.insert(newStatus);
        } else {
            status.setCurrentTime(new Date());
            status.setIsOnline(inOnline);
            return systemStatusMapper.updateById(status);
        }
    }

    @Override
    public void updateStatusByDate() {
        HashMap<String, Object> map = new HashMap<>(2);
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date());
        int number = Integer.parseInt(timeLong);
        calendar.add(Calendar.SECOND, -number);
        map.put("timeLong", calendar.getTime());
        map.put("status", "1");
        map.put("viidServerId", viidRunner.getSelfViidServerId());
        systemStatusMapper.updateStatusByDate(map);
    }

    @Override
    public void updateSystemStatus(String inOnline, String viidServerId) {
        SystemStatus status = systemStatusMapper.findByVIIDServerID(viidServerId);
        if (!ObjectUtil.isNull(status)) {
            status.setCurrentTime(new Date());
            status.setIsOnline(inOnline);
            systemStatusMapper.updateById(status);
        }
    }

    @Override
    public void updateApeStatusByDate() {
        HashMap<String, Object> map = new HashMap<>(2);
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date());
        int number = Integer.parseInt(timeLong);
        calendar.add(Calendar.SECOND, -number);
        map.put("timeLong", calendar.getTime());
        map.put("status", "2");
        apeStatusMapper.updateApeStatusByDate(map);
    }

    @Override
    public int updateAndInsertApeStatus(String inOnline, String deviceID) {
        ApeStatus status = apeStatusMapper.findByApeID(deviceID);
        if (ObjectUtil.isNull(status)) {
            ApeStatus newStatus = new ApeStatus();
            newStatus.setId(IdUtil.fastSimpleUUID());
            newStatus.setApeID(deviceID);
            newStatus.setIsOnline(inOnline);
            newStatus.setCurrentTime(new Date());
            return apeStatusMapper.insert(newStatus);
        } else {
            status.setCurrentTime(new Date());
            status.setIsOnline(inOnline);
            return apeStatusMapper.updateById(status);
        }
    }
}
